/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package modeles.beans;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author Fred
 */
@ManagedBean
@ViewScoped
public class ConnexionBean {
    
    @ManagedProperty("#{loginBean}")
    private LoginBean loginService;
    private final String redirect;
    private String login;
    private String mdp;

    /**
     * Creates a new instance of ConnexionBean
     */
    public ConnexionBean() {
        redirect = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("redirect");
    } 
    
    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getMdp() {
        return mdp;
    }

    public void setMdp(String mdp) {
        this.mdp = mdp;
    }

    public void setLoginService(LoginBean loginService) {
        this.loginService = loginService;
    }
    
    public String connecter(){
        if (loginService.login(login, mdp)) {                      
            if (redirect != null) {                
                return redirect + "?faces-redirect=true";
            }
            return "accueil.xhtml?faces-redirect=true";
        } else {
            FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Authentification refusée: Login ou mot de passe erroné.", null);
            FacesContext.getCurrentInstance().addMessage(null, msg);
            return "connexion.xhtml";
        }
    }
}
